﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QUT.Bio.Util {
	/// <summary>
	/// A readonly indexer backed by a dictionary.
	/// </summary>
	/// <typeparam name="IndexType">The index type of the indexer.</typeparam>
	/// <typeparam name="OutputType">The reuslt type of the indexer.</typeparam>

	public class DictionaryBackedIndexer<IndexType,OutputType>: ReadonlyIndexer<IndexType,OutputType> {
		private Dictionary<IndexType,OutputType> dictionary;
		
		/// <summary>
		/// Initialise a DictionaryBackedIndexer.
		/// </summary>
		/// <param name="dictionary">The dictionary instance that backs this indexer.</param>
		
		public DictionaryBackedIndexer( Dictionary<IndexType,OutputType> dictionary ) {
			this.dictionary = dictionary;
		}
		
		/// <summary>
		/// Gets the output value associated with a given key.
		/// </summary>
		/// <param name="i">The key.</param>
		/// <returns></returns>
		
		public OutputType this[IndexType i] {
			get {
				return dictionary[i];
			}
		}
		
		/// <summary>
		/// Gets an enumerator.
		/// </summary>
		/// <returns>an enumerator.</returns>
		
		public IEnumerator<OutputType> GetEnumerator () {
			return dictionary.Values.GetEnumerator();
		}
		
		/// <summary>
		/// Gets an enumerator.
		/// </summary>
		/// <returns>an enumerator.</returns>

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {
			return dictionary.Values.GetEnumerator();
		}
	}
}
